package com.sudichina.sudichina.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SpUtils {
	private static SharedPreferences sp;

	/**
	 * 避免反复打开关闭SP
	 *
	 * @param context
	 * @return
	 */
	private static SharedPreferences getSharedPref(Context context) {
		if (sp == null) {
			sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		}
		return sp;
	}

	/*
     * 往sp里存布尔值
     */
	public static void putBoolean(Context context, String key, boolean value) {
		getSharedPref(context).edit().putBoolean(key, value).commit();
	}

	/*
     * 从sp里取出布尔值
     */
	public static boolean getBoolean(Context context, String key,
									 boolean defValue) {
		return getSharedPref(context).getBoolean(key, defValue);
	}

	/*
     * 往sp里存String值
     */
	public static void putString(Context context, String key, String value) {
		getSharedPref(context).edit().putString(key, value).commit();
	}

	/*
     * 从sp里取出String值
     */
	public static String getString(Context context, String key, String defValue) {
		return getSharedPref(context).getString(key, defValue);
	}

	/*
     * 移除一个字符串
     */
	public static void removeString(Context context, String key) {
		getSharedPref(context).edit().remove(key).commit();

	}

	/**
	 * 移除一个Int值
	 *
	 * @param context
	 * @param key
	 */
	public static void removeInt(Context context, String key) {
		getSharedPref(context).edit().remove(key).commit();
	}


	/*
     * 移除一个字符串
     */
	public static void removeBoolean(Context context, String key) {
		getSharedPref(context).edit().remove(key).commit();

	}

	public static void putInt(Context context, String key, int value) {
		getSharedPref(context).edit().putInt(key, value).commit();
	}

	public static int getInt(Context context, String key) {
		return getSharedPref(context).getInt(key, 0);
	}
}
